﻿using BwTMusicModels;
using BwTServices.Album;
using BwTServices.Country;
using BwTServices.Genre;
using BwTServices.Music;
using BwTServices.Singer;
using BwTServices.Song;
using BwTServices.Theme;
using HelperModels;
using HelperModels.Album;
using HelperModels.Singer;
using HelperModels.Songs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BwTAdmin.Controllers
{
    public class MusicController : Controller
    {
        MusicServices _services = new MusicServices();
        ThemeServices _theme = new ThemeServices();
        GenreServices _genre = new GenreServices();
        SingerServices _singer = new SingerServices();
        CountryServices country = new CountryServices();
        AlbumServices _album = new AlbumServices();
        SongServices _song = new SongServices();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SingerManager()
        {
            return View();
        }
        public ActionResult AlbumManager()
        {
            return View();
        }
        public ActionResult SongManager()
        {
            return View();
        }
        public JsonResult GetAllMusic(MusicFilter P)
        {
            return Json(new { data = _services.GetAllNorthMusic(P, out int total), statusCode = 1, message = "查询成功", success = true, totalCount = total }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllThemes()
        {
            return Json(new { data = _theme.GetAllThemes(), statusCode = 1, message = "查询成功", success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllGenres()
        {
            return Json(new { data = _genre.GetAllGenres(), statusCode = 1, message = "查询成功", success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult RemoveMusic(MusicHelper helper)
        {
            var res = _services.IsNoVaildForMusic(helper.Musics);
            int falg = 1;
            string msg = "删除成功";
            if (res!=1)
            {
                falg = res;
                msg = "删除失败";
            }
            return Json(new { statusCode = falg, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllSinger(SingerFilter P)
        {
            return Json(new { data = _singer.GetAllSingerList(P, out int total), statusCode = 1, message = "查询成功", success = true, totalCount = total }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllAlbums(AlbumFilter P)
        {
            return Json(new { data = _album.GetAllAlbumList(P, out int total), statusCode = 1, message = "查询成功", success = true, totalCount = total }, JsonRequestBehavior.AllowGet);
        }
        
        public JsonResult GetAllCountry()
        {
            return Json(new { data = country.GetAllCountryList(), statusCode = 1, message = "查询成功", success = true}, JsonRequestBehavior.AllowGet);
        }
        public JsonResult UpdateSingerInfo(North_Singer singer)
        {
            var res = _singer.UpdateSingerInfo(singer);
            int falg = 1;
            string msg = "修改成功";
            if (res != 1)
            {
                falg = res;
                msg = "修改失败";
            }
            return Json(new { statusCode = falg, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetAllSongsList(SongFilter P)
        {
            return Json(new { data = _song.GetAllSongsList(P, out int total), statusCode = 1, message = "查询成功", success = true, totalCount = total }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetExistSongList(string SongListMusic)
        {
            List<MusicHelper> musics = new List<MusicHelper>();
            string[] ids = SongListMusic.Split(',');
            for (int i = 0; i < ids.Length; i++)
            {
                musics.Add(_services.GetMusicHelperInfo(Convert.ToInt32(ids[i])));
            }
            return Json(new { data = musics, statusCode = 1, message = "查询成功", success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult ModifySongList(North_Songs info, List<MusicHelper> list)
        {
            string str = "";
            List<string> strs = new List<string>();
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    strs.Add(list[i].Musics.MusicId.ToString());
                }
                str = string.Join(",", strs.ToArray());

            }
            info.SongListMusic = str;
            info.SongListAuthor = Guid.Parse("0df976a7-b339-4112-a2a6-cc166ed18075");
            info.SongListTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            var res =_song.ModifySongList(info);
            int falg = 1;
            string msg = "修改成功";
            if (res != 1)
            {
                falg = res;
                msg = "修改失败";
            }
            return Json(new { statusCode = falg, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult AddSongList(North_Songs info,List<MusicHelper> list)
        {
            string str = "";
            List<string> strs = new List<string>();
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    strs.Add(list[i].Musics.MusicId.ToString());
                }
                 str = string.Join(",", strs.ToArray());

            }
            info.SongListMusic = str;
            info.SongListAuthor =Guid.Parse("0df976a7-b339-4112-a2a6-cc166ed18075");
            info.SongListTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            var res = _song.AddSongList(info);
            int falg = 1;
            string msg = "添加成功";
            if (res != 1)
            {
                falg = res;
                msg = "添加失败";
            }
            return Json(new { statusCode = falg, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult RemoveSongList(int id)
        {
            var res = _song.RemoveSongList(id);
            int falg = 1;
            string msg = "删除成功";
            if (res != 1)
            {
                falg = res;
                msg = "删除失败";
            }
            return Json(new { statusCode = falg, message = msg, success = true }, JsonRequestBehavior.AllowGet);
        }
    }
}